<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="java.sql.*" %>
<html>

<head>
    <meta charset="utf-8">
    <title>Bootstrap 布局实例</title>
    <link rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/css/bootstrap.min.css">
    <script src="https://cdn.staticfile.org/jquery/2.1.1/jquery.min.js"></script>
    <script src="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/js/bootstrap.min.js"></script>
    <script src="js/user.js"></script>
    <link rel="stylesheet" href="css/user.css">
    <style>
        td, th {
            text-align: center;
        }

        .custom-smaller-input {
            font-size: 1.25rem;
        }
    </style>
</head>

<body>
<div class="container-fluid">
    <div class="row">
        <div class="col-sm-12" id="right">
            <ol class="breadcrumb">
                <li class="active">菜单
                </li>
                <li class="active">产品信息
                </li>
            </ol>
            <%
                String productCode = "";
                String productName = "";
                String batch = "";
                String worker = "";
                String processingDate = "";
                String workshop = "";
                String productSize = "";
                String processingMethod = "";

                if (request.getParameter("productCode") != null) productCode = request.getParameter("productCode");
                if (request.getParameter("productName") != null) productName = request.getParameter("productName");
                if (request.getParameter("batch") != null) batch = request.getParameter("batch");
                if (request.getParameter("worker") != null) worker = request.getParameter("worker");
                if (request.getParameter("processingDate") != null) processingDate = request.getParameter("processingDate");
                if (request.getParameter("workshop") != null) workshop = request.getParameter("workshop");
                if (request.getParameter("productSize") != null) productSize = request.getParameter("productSize");
                if (request.getParameter("processingMethod") != null) processingMethod = request.getParameter("processingMethod");
            %>
            <div class="panel panel-default">
                <div class="panel-heading">
                    搜索
                </div>
                <div class="panel-body">
                    <form role="form" class="form-inline" action="product_list.jsp" method="post">
                        <div class="form-group">
                            <label>产品身份码</label>
                            <input type="text" class="form-control custom-smaller-input" id="productCode"
                                   name="productCode" value="<%=productCode%>" placeholder="请输入产品身份码">
                        </div>
                        <div class="form-group">
                            <label>产品名称</label>
                            <input type="text" class="form-control custom-smaller-input" id="productName" name="productName"
                                   value="<%=productName%>" placeholder="请输入产品名称">
                        </div>
                        <div class="form-group">
                            <label>批次</label>
                            <input type="text" class="form-control custom-smaller-input" id="productSize"
                                   name="productName"
                                   value="<%=batch%>">
                        </div>
                        <div class="form-group">
                            <label>加工人员</label>
                            <input type="text" class="form-control custom-smaller-input" id="worker"
                                   name="worker"
                                   value="<%=worker%>" placeholder="请输入加工人员名称">
                        </div>
                        <div class="form-group">
                            <label>加工车间</label>
                            <input type="text" class="form-control custom-smaller-input" id="workshop"
                                   name="workshop" value="<%=workshop%>" placeholder="请输入加工车间">
                        </div>
                        <div class="form-group">
                            <button type="submit" class="btn btn-primary">开始搜索</button>
                        </div>
                        <div class="form-group">
                            <a class="btn btn-default" href="product_add.jsp">添加</a>
                        </div>
                    </form>
                </div>
            </div>
            <!--
            列表展示
        -->
            <div class="table-responsive">
                <table class="table table-striped ">
                    <thead>
                    <tr>
                        <%--<th>序号</th>--%>
                            <th>产品身份码</th>
                            <th>产品名称</th>
                            <th>外观尺寸</th>
                            <th>批次</th>
                            <th>加工人员</th>
                            <th>加工方法</th>
                            <th>加工时间</th>
                            <th>加工车间</th>
                            <th>操作</th>
                    </tr>
                    </thead>
                    <tbody>
                    <%
                        Connection conn = (Connection) session.getAttribute("Connection");
                        String sql = "SELECT * FROM product WHERE 1=1";
                        if (!productCode.isEmpty()) {
                            sql += " AND productCode LIKE '%" + productCode + "%'";
                        }
                        if (!productName.isEmpty()) {
                            sql += " AND productName = '" + productName + "'";
                        }
                        if (!productSize.isEmpty()) {
                            sql += " AND productSize = '" + productSize + "'";
                        }
                        if (!worker.isEmpty()) {
                            sql += " AND worker = '" + worker + "'";
                        }
                        if (!batch.isEmpty()) {
                            sql += " AND batch = '" + batch + "'";
                        }
                        int curpage = 1;
                        if (request.getParameter("page") != null) {
                            String pg = request.getParameter("page");
                            curpage = Integer.parseInt(pg);
                        }
                        int al = 0; // al为-1时表示上一页，为1时表示下一页，默认为0
                        if (request.getParameter("alterpage") != null) {
                            String alpg = request.getParameter("alterpage");
                            al = Integer.parseInt(alpg);
                        }
                        if (curpage > 1 || al > 0) {
                            curpage += al;
                        }
                        int page_size = 6;
                        int start = curpage * page_size - page_size;
                        String pge = " LIMIT " + start + ", " + page_size;  // 注意前面添加了一个空格,没这个空格就会报错
                        sql = sql + pge;

                        PreparedStatement pstmt = conn.prepareStatement(sql);
                        ResultSet rs = pstmt.executeQuery();



                        while (rs.next()) {
                    %>
                    <tr>
                        <td><%=rs.getString("productCode")%>
                        </td>
                        <td><%=rs.getString("productName")%>
                        </td>
                        <td><%=rs.getString("productSize")%>
                        </td>
                        <td><%=rs.getString("batch")%>
                        </td>
                        <td><%=rs.getString("worker")%>
                        </td>
                        <td><%=rs.getString("processingMethod")%>
                        </td>
                        <td><%=rs.getString("processingDate")%>
                        </td>
                        <td><%=rs.getString("workshop")%>
                        </td>
                        <td>
                            <div class="btn-group">
                                <a class="btn btn-default" href="product_edit.jsp?id=<%=rs.getInt("id")%>">修改</a>
                                <a class="btn btn-danger"
                                   href="#" onclick="confirmDelete(<%=rs.getInt("id")%>)">删除</a>
                            </div>
                        </td>
                    </tr>
                    <%
                        }
                    %>
                    </tbody>
                </table>
            </div>
            <ul class="pagination" style="float: right;">
                <li>
                    <a href="product_list.jsp?alterpage=-1&page=<%=curpage%>&productCode=<%=productCode%>&productName=<%=productName%>&productSize=<%=productSize%>&worker=<%=worker%>&batch=<%=batch%>">&laquo;</a>
                </li>
                <%
                    int psize = 5; //页面号码标签数量
                    int ppg = psize * ((curpage - 1) / psize + 1);
                    for (int i = ppg - psize + 1; i <= ppg; i++) {
                        if (i == curpage) {
                            out.println("<li class=\"active\"><a href=\"product_list.jsp?page=" + i + "&productCode=" + productCode + "&productName=" + productName + "&productSize=" + productSize + "&worker=" + worker + "&batch=" + batch + "\">" + i + "</a></li>");

                        } else {
                            out.println("<li><a href=\"product_list.jsp?page=" + i + "&productCode=" + productCode + "&productName=" + productName + "&productSize=" + productSize + "&worker=" + worker + "&batch=" + batch + "\">" + i + "</a></li>");
                        }
                    }
                %>
                <li>
                    <a href="product_list.jsp?alterpage=1&&page=<%=curpage%>&productCode=<%=productCode%>&productName=<%=productName%>&productSize=<%=productSize%>&worker=<%=worker%>&batch=<%=batch%>">&raquo;</a>
                </li>
            </ul>
        </div>
    </div>
</div>

</body>
<script>
    function confirmDelete(id) {
        if (confirm("确定要删除吗？")) {
            window.location.href = "product_delete.jsp?id="+id+"&page=<%=curpage%>&productCode=<%=productCode%>&productName=<%=productName%>&productSize=<%=productSize%>&worker=<%=worker%>&batch=<%=batch%>";
        }
    }
</script>
</html>